<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${packagePrivilege == null ? '添加' : '编辑'}套餐权限</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head><body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/charge/packagePrivilege/save.do">
                    <input type="hidden" name="packageId" value="${dbPackage.id}" />
                    <input type="hidden" name="id" value="${packagePrivilege.id}" />
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">产品权限<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <select name="privilegeParentId" lay-filter="parent_privilege_select" lay-verify="p_privilege">
                                    <option value="">请选择父级</option>
                                    <c:forEach var="privilege" items="${privileges}">
                                        <option value="${privilege.id}" ${productPrivilege.parentId == privilege.id ? 'selected="selected"' : ''}>${privilege.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select name="privilegeId" lay-verify="c_privilege" id="privilegeSelect">
                                    <option value="">请选择子级</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否为数字<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <input type="radio" name="isNumber" lay-filter="required" value="1" title="是"
                            ${packagePrivilege == null || packagePrivilege.isNumber == 1 ? 'checked="checked"' : ''}
                            >
                            <input type="radio" name="isNumber" lay-filter="required" value="2" title="否"
                            ${packagePrivilege != null && packagePrivilege.isNumber == 2 ? 'checked="checked"' : ''}
                            >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">参数值<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="paramValue" lay-verify="paramValue" class="layui-input" value="${packagePrivilege.paramValue}"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否有效<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <input type="radio" name="delFlag" lay-filter="required" value="0" title="是"
                            ${packagePrivilege == null || packagePrivilege.delFlag == '0' ? 'checked="checked"' : ''}
                            >
                            <input type="radio" name="delFlag" lay-filter="required" value="1" title="否"
                            ${packagePrivilege != null && packagePrivilege.delFlag == '1' ? 'checked="checked"' : ''}
                            >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">排序号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sort" lay-verify="sort" class="layui-input" value="${productPackage.sort}"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
                            <a href="javascript:history.back();" class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>

<script>
    $(function () {
        layui.use(['form'], function () {
            var form = layui.form;

            //自定义验证规则
            form.verify({
                paramValue: function (value) {
                    var isNumber = $("[name=isNumber]:checked").val();
                    if (isNumber == "1") {
                        if (value == "") {
                            return '请输入参数值';
                        } else if (!util.checkNumber(value)) {
                            return "请输入正确的数字";
                        }
                    }
                },
                sort: function (value) {
                    if (!util.checkNumber(value)) {
                        return "请输入正确的数字";
                    }
                }
            });

            // 父级
            form.on('select(parent_privilege_select)', function (data) {
                loadChildrenData(data.value);
            });

            if ('${productPrivilege.parentId}' != '') {
                loadChildrenData('${productPrivilege.parentId}');
            }

            // 加载子级数据
            function loadChildrenData(parentId) {
                $.post("${ctx}/charge/privilege/findByParentId.do", {"parentId": parentId}, function (res) {
                    var array = new Array();
                    for (var i = 0; i < res.data.length; i++) {
                        var privilege = res.data[i];
                        array.push('<option value="' + privilege.id + '">' + privilege.name + '</option>');
                    }
                    $("#privilegeSelect").html("").append(array.join(""));
                    form.render('select');
                });
            }

            //监听提交
            form.on('submit', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    var action = $("#myForm").attr("action");
                    $.post(action, $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                location.href = "${ctx}/charge/packagePrivilege/list.do?packageId=" + res.data;
                            });
                        } else {
                            util.layerMsgError("提交失败")
                        }
                    });
                })
                return false;
            });
        });
    })
</script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '套餐管理',
            href: '${ctx}/charge/package/packageList.do'
        },{
            name: '${dbPackage.name}',
            href: 'javascript:history.back();'
        },{
            name: '${packagePrivilege == null ? '添加' : '编辑'}套餐权限',
            curr: true
        }],
        btns:[{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</body>
</html>